using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using HappySprite;
using HappySprite.Simulation;

namespace HappyInvaders
{
    class BulletEntity : Entity
    {
        public BulletEntity()
        {
            Item.CollisionShape = Box.CreateFromSize(Constants.BulletWidth, Constants.BulletHeight);
            Item.CollisionGroup = Constants.BulletGroup;
            Item.CollisionMask = Constants.BombGroup | Constants.ShelterGroup | Constants.InvaderGroup | Constants.SaucerGroup;

            Item.SetWorldLimits(0f, 0f, Constants.WorldWidth, Constants.WorldHeight);
            Item.VelocityY = Constants.BulletSpeed;

            // Handle world limits
            Item.WorldLimitsExceeded += Remove;

            // Handle collision
            Item.Collision += CollisionHandler;
        }

        internal override void OnAdd()
        {
            Owner.BulletCount++;
        }

        internal override void OnRemove()
        {
            Owner.BulletCount--;
        }

        void CollisionHandler(CollisionEventArgs e)
        {
            if (e.Other.Tag is ShelterEntity)
            {
                // Check for an actual collision
                ShelterEntity shelter = (ShelterEntity)e.Other.Tag;
                if (!shelter.CheckBullet(e.This.Transform.Position))
                    return;
            }

            Remove();
        }
    }
}
